package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.PicDetailActivity;
import flc.ast.adapter.RecordAdapter;
import flc.ast.databinding.FragmentFileBinding;
import flc.ast.dialog.EditDialog;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.IntentUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FileFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FileFragment extends BaseNoModelFragment<FragmentFileBinding> {

    private int type = 0;
    private List<String> listShow = new ArrayList<>();
    private RecordAdapter recordAdapter;
    private BasePopupView popupView;
    private EditDialog editDialog;
    private int selPosition;

    @Override
    protected int onCreate() {
        return R.layout.fragment_file;
    }

    @Override
    protected void initView() {
        mDataBinding.llCompressRecord.setOnClickListener(this);
        mDataBinding.llCreateRecord.setOnClickListener(this);

        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(mContext));
        recordAdapter = new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivRecordItemEdit);
        recordAdapter.setOnItemClickListener(this);
        recordAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llCompressRecord://压缩记录
                clearSelection();
                mDataBinding.tvCompressRecord.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.ivCompressRecord.setVisibility(View.VISIBLE);
                type = 0;
                getCompressData();
                break;
            case R.id.llCreateRecord://创作记录
                clearSelection();
                mDataBinding.tvCreateRecord.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.ivCreateRecord.setVisibility(View.VISIBLE);
                type = 1;
                getCreateData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        PicDetailActivity.listPath = recordAdapter.getValidData();
        PicDetailActivity.selPosition = position;
        startActivity(PicDetailActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivRecordItemEdit:
                selPosition = position;
                editDialog(view);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (type == 0) {
            getCompressData();
        } else if (type == 1) {
            getCreateData();
        }
    }


    private void getCompressData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myCompress;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        Collections.sort(listShow, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                long t1 = FileUtils.getFileLastModified(o1);
                long t2 = FileUtils.getFileLastModified(o2);
                return t2 > t1 ? 1 : -1;
            }
        });
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.tvRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.tvRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getCreateData() {
        listShow.clear();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myCreate;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        Collections.sort(listShow, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                long t1 = FileUtils.getFileLastModified(o1);
                long t2 = FileUtils.getFileLastModified(o2);
                return t2 > t1 ? 1 : -1;
            }
        });
        if (listShow.size() > 0) {
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
            mDataBinding.tvRecordNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
            mDataBinding.tvRecordNoData.setVisibility(View.VISIBLE);
        }
    }

    //颜色弹框
    private void editDialog(View view) {
        editDialog = new EditDialog(mContext);
        editDialog.setListener(new EditDialog.IListener() {
            @Override
            public void onClickDelete() {
                FileUtils.delete(recordAdapter.getItem(selPosition));
                ToastUtils.showShort(getString(R.string.delete_suc));
                if (type == 0) {
                    getCompressData();
                } else {
                    getCreateData();
                }
                popupView.dismiss();
            }

            @Override
            public void onClickShare() {
                IntentUtil.shareImage(mContext, getString(R.string.share), recordAdapter.getItem(selPosition));
                popupView.dismiss();
            }
        });
        popupView = new XPopup.Builder(mContext)
                .popupPosition(PopupPosition.Left) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(view)
                .hasShadowBg(false) // 去掉半透明背景
                .asCustom(editDialog);
        popupView.show();
    }

    private void clearSelection() {
        mDataBinding.tvCompressRecord.setTextColor(Color.parseColor("#2EFFFFFF"));
        mDataBinding.ivCompressRecord.setVisibility(View.INVISIBLE);
        mDataBinding.tvCreateRecord.setTextColor(Color.parseColor("#2EFFFFFF"));
        mDataBinding.ivCreateRecord.setVisibility(View.INVISIBLE);

    }

}